﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.InfoSys
{
    public partial class ResetPasswordForm : BaseForm
    {
        #region 成员函数
        public SysAccount CurrentSysAccount { get; set; }
        #endregion

        #region 构造函数
        public ResetPasswordForm(String strUserCode)
        {
            InitializeComponent();
            
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            SysAccount record = new SysAccount();
            record.Id = strUserCode;
            CurrentSysAccount = client.QuerySysAccount(record);
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitControl()
        {
            this.tbUserCode.Text = this.CurrentSysAccount.Id;
            this.tbUserName.Text = this.CurrentSysAccount.UserName;
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.tbPassword.Text.Trim() == "")
            {
                bRet = false;
                tbPassword.BackColor = Color.Yellow;
            }
            if (this.tbPasswordCfm.Text.Trim() == "")
            {
                bRet = false;
                tbPasswordCfm.BackColor = Color.Yellow;
            }

            if (!bRet)
            {
                return;
            }

            if (this.tbPassword.Text.Trim().CompareTo(this.tbPasswordCfm.Text.Trim()) != 0)
            {
                MessageBox.Show("初始密码与确认密码不相符，请重新输入！");
                return;
            }
            if (MessageBox.Show("您确定要添加该用户吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                SysAccount record = new SysAccount();
                record = this.CurrentSysAccount;
                record.Password = GetHashValue.Cryptography.GetHashValue(this.tbPassword.Text.Trim());
                ServiceReferenceZwms.ServiceZwmsClient sysClient = new ServiceReferenceZwms.ServiceZwmsClient();
                bRet = sysClient.UpdateSysAccount(record);
                if (bRet)
                {
                    MessageBox.Show("密码重置成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("密码重置失败！");
                    return;
                }

            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion

        

    }
}
